package com.quectel.core.module.grid.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.grid.dao.GridMemberWorkLogDao;
import com.quectel.core.module.grid.dto.GridDto;
import com.quectel.core.module.grid.dto.GridMemberDto;
import com.quectel.core.module.grid.dto.GridMemberWorkLogDto;
import com.quectel.core.module.grid.entity.GridMemberWorkLogEntity;
import com.quectel.core.module.grid.service.GridMemberService;
import com.quectel.core.module.grid.service.GridMemberWorkLogService;
import com.quectel.core.module.grid.service.GridService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/02/03 16:42
 */
@DubboService
public class GridMemberWorkLogServiceImpl implements GridMemberWorkLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(GridMemberWorkLogServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> GRID_MEMBER_WORK_LOG_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "GRID_MEMBER_WORK_LOG_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<GridMemberWorkLogDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(GRID_MEMBER_WORK_LOG_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private GridMemberWorkLogDao gridMemberWorkLogDao;
    @Autowired
    private GridService gridService;

    @Autowired
    private GridMemberService gridMemberService;


    @Override
    public List<Date> logDates(Map<String, Object> params) {
        return gridMemberWorkLogDao.logDates(params);
    }


    @Override
    public GridMemberWorkLogDto selectByIdWithNoPadding(Long id) {
        GridMemberWorkLogEntity entity = gridMemberWorkLogDao.selectById(id);
        return CopyUtils.copyObj(entity, GridMemberWorkLogDto.class);
    }

    @Override
    public GridMemberWorkLogDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                GRID_MEMBER_WORK_LOG_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public GridMemberWorkLogDto selectById(Long id) {
        GridMemberWorkLogEntity entity = gridMemberWorkLogDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, GridMemberWorkLogDto.class));
    }

    @Override
    public List<GridMemberWorkLogDto> queryListWithNoPadding(Map<String, Object> params) {
        List<GridMemberWorkLogEntity> list = gridMemberWorkLogDao.queryList(params);
        List<GridMemberWorkLogDto> result = CopyUtils.copyList(list, GridMemberWorkLogDto.class);
        return result;
    }

    @Override
    public List<GridMemberWorkLogDto> queryList(Map<String, Object> params) {
        List<GridMemberWorkLogEntity> list = gridMemberWorkLogDao.queryList(params);
        List<GridMemberWorkLogDto> result = CopyUtils.copyList(list, GridMemberWorkLogDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属社区等
     *
     * @param dto
     * @return
     */
    private GridMemberWorkLogDto paddingField(GridMemberWorkLogDto dto) {
        if (dto != null) {
            GridDto gridDto = gridService.selectCacheById(dto.getGridId());
            dto.setGrid(gridDto);

            GridMemberDto gridMemberDto = gridMemberService.selectCacheById(dto.getGridMemberId());
            if (gridMemberDto != null) {
                //脱敏
                gridMemberDto.setPassword(null);
                gridMemberDto.setCreator(null);
                gridMemberDto.setCreateTime(null);
                gridMemberDto.setEditor(null);
                gridMemberDto.setEditTime(null);
                dto.setGridMember(gridMemberDto);
            }

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return gridMemberWorkLogDao.queryTotal(params);
    }

    @Override
    public Long save(GridMemberWorkLogDto dto) {
        GridMemberWorkLogEntity entity = CopyUtils.copyObj(dto, GridMemberWorkLogEntity.class);
        entity.setId(Snowflake.nextId());
        gridMemberWorkLogDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(GridMemberWorkLogDto dto) {
        GridMemberWorkLogEntity entity = CopyUtils.copyObj(dto, GridMemberWorkLogEntity.class);

        gridMemberWorkLogDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(GridMemberWorkLogDto dto) {
        GridMemberWorkLogEntity entity = CopyUtils.copyObj(dto, GridMemberWorkLogEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, gridMemberWorkLogDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        gridMemberWorkLogDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
